package com.gton.util;


import com.baomidou.mybatisplus.core.toolkit.AES;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @description: MybatisPlus加密数据库明文配置
 * @author: GuoTong
 * @createTime: 2022-09-24 13:50
 * @since JDK 1.8 OR 11
 **/

public class MybatisPlusPasswordAES {

    Logger logger = LoggerFactory.getLogger(MybatisPlusPasswordAES.class);

    private String randomKey = "";


    public String getRandomKey() {
        String randomKey = AES.generateRandomKey();
        logger.info("数据库加密秘钥------>{}", randomKey);
        this.randomKey = randomKey;
        return randomKey;
    }

    /**
     * Description:  签名：数据库层次实际加密，不可逆
     *
     * @author: GuoTong
     */
    public String decryptStringValue(String msg) {
        String data = msg;
        // 随机密钥加密
        if (StringUtils.isEmpty(this.randomKey)) {
            this.randomKey = getRandomKey();
        }
        String result = AES.encrypt(data, this.randomKey);
        return result;
    }


    public static void mainFunction() {
        String root = new MybatisPlusPasswordAES().decryptStringValue("root");
        System.out.println(root);
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        String format = now.format(formatter);
        System.out.println(format);
    }

}
